public class Test {

    //位运算

    //消失的两个数字
    public int[] missingTwo(int[] nums)
    {
        // 1. 先把所有的数异或在⼀起
        int tmp = 0;
        for(int x : nums) tmp ^= x;
        for(int i = 1; i <= nums.length + 2; i++) tmp ^= i;
        // 2. 找出 a，b 两个数⽐特位不同的那⼀位
        int diff = 0;
        while(true)
        {
            if(((tmp >> diff) & 1) == 1) break;
            else diff++;
        }
        // 3. 将所有的数按照 diff 位不同，分两类异或
        int[] ret = new int[2];
        for(int x : nums)
            if(((x >> diff) & 1) == 1) ret[1] ^= x;
            else ret[0] ^= x;
        for(int i = 1; i <= nums.length + 2; i++)
            if(((i >> diff) & 1) == 1) ret[1] ^= i;
            else ret[0] ^= i;
        return ret;
    }


    //只出现⼀次的数字 II
    public int singleNumber(int[] nums){
        int ret = 0;
        for (int i = 0; i < 32; i++) {
            int sum = 0;
            for (int x: nums) {
                if (((x >>i) & 1) == 1) sum++;
            }
            sum %= 3;
            if (sum == 1) ret |= 1 << i;
        }
        return ret;
    }



    //两数之和
    public int getSum(int a, int b) {
        while (b != 0){
            int x = a ^ b;
            int carry = (a & b) << 1;
            a = x;
            b = carry;
        }
        return a;
    }





    //丢失的数字
    public int missingNumber(int[] nums) {
        int ret = 0;
        for (int x: nums) {
            ret^=x;
        }
        for (int i = 0; i < nums.length; i++) {
            ret^= i;
        }
        return ret;
    }





    //判断字符是否唯⼀
    public boolean isUnique(String astr) {

        if (astr.length() > 26) return false;

        int binMap = 0;
        for (int i = 0; i < astr.length(); i++) {
            int x = astr.charAt(i)-'a';
            if (((binMap >> x)& 1 ) == 1) return false;

            binMap |= 1 << x;
        }
        return true;
    }



}
